<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/18 0018
 * Time: 15:59
 */

namespace Admin\Controller;



use Admin\Model\GoodsModel;

class AdvertisingController extends CommonController{

    public function index(){

        $rel  = D('adverti_list as a ')->field('a.adverti_id,a.adverti_class_id,a.image,a.describe,a.url,a.state,
        a.sort,b.class_name')->join('left join agency_adverti_class as  b on a.adverti_class_id = b.adverti_class_id')
            ->order("a.adverti_class_id asc,a.sort desc")->select();

        $this->assign('list',$rel);
        $this->display();
    }

    public function add(){

        $data = I('post.');
        //图片上传
        $img =new GoodsModel();
        $res = $img->uploadImg();

        if($res){
            $data['image'] = "http://".$_SERVER['HTTP_HOST']."/".$res['goods_img'];
        }

        //die(json_encode(array('rel'=>$data)));

        $rel  = D('adverti_list')->add($data);
        if($rel){
           $this->success("添加成功",U("advertising/index"),3);
           die;
        }


        $cate = M('adverti_class')->where(array('state'=>1))->select();

        $this->assign('cate',$cate);
        $this->display();
    }
    function dels(){
        $role=I("get.role_id");
        $tableName=I("get.tableName");
        $data['state']=0;
        if($tableName=='adverti_list'){
            $result=M("adverti_list")->where("adverti_id='".$role."'")->save($data);
            if($result!=false){
                $this->success("删除成功！",U('advertising/index'),3);
            }else{
                $this->error("删除失败！",U("advertising/index"),3);
            }
        }elseif ($tableName=='adverti_class'){
            $result=M("adverti_class")->where("adverti_class_id='".$role."'")->save($data);
            if($result!=false){
                $this->success("删除成功！",U('adcertisingClass/index'),3);
            }else{
                $this->error("删除失败！",U("adcertisingClass/index"),3);
            }
        }
    }
    function edit(){
        $role=I("get.role_id");
        $tableName=I("get.tableName");
        if($tableName=='adverti_list'){
            $result=M("$tableName")->where("adverti_id='".$role."'")->select();
            foreach($result as $v){
                $adverti_class_id=$v['adverti_class_id'];
                $image=$v['image'];
                $describe=$v['describe'];
                $url=$v['url'];
                $state=$v['state'];
                $order=$v['order'];
                $this->assign('adverti_id',$role);
                $this->assign('adverti_class_id',$adverti_class_id);
                $this->assign('image',$image);
                $this->assign('describe',$describe);
                $this->assign('url',$url);
                $this->assign('state',$state);
                $this->assign('order',$order);
            }
        }elseif($tableName=='adverti_class'){
            $result=M("$tableName")->where("adverti_class_id='".$role."'")->find();
           $this->assign('result',$result);
          /*  foreach($result as $v){
                $class_name=$v['class_name'];
                $state=$v['state'];
                $memo=$v['memo'];

                $this->assign('adverti_class_id',$role);
                $this->assign('class_name',$class_name);
                $this->assign('state',$state);
                $this->assign('memo',$memo);
            }*/
        }
//        var_dump($role,$class_name,$state,$memo);
        $cate = M('adverti_class')->where(array('state'=>1))->select();
        $this->assign('cate',$cate);
        $this->display("edit.html$tableName");
    }
    function editadverti_list(){
        $arr=I("post.");

        //图片上传
        $img =new GoodsModel();
        $res = $img->uploadImg();

        if($res){
            $arr['image'] = "http://".$_SERVER['HTTP_HOST']."/".$res['goods_img'];
        }

        $id=$arr['adverti_id'];
        if($id){
            $result=M("adverti_list")->where("adverti_id='".$id."'")->save($arr);
            if($result!=false){
                $this->success("更新成功！",U('Advertising/index'),3);
            }else{
                $this->error("更新失败！");
            }
        }
    }
    function editadverti_class(){
        $arr=I("post.");
        $id=$arr['adverti_class_id'];

        if($id){
            $result=M("adverti_class")->where("adverti_class_id='".$id."'")->save($arr);
            if($result!=false){
                $this->success("更新成功！",U('adcertisingClass/index'),3);
            }else{
                $this->error("更新失败！");
            }
        }
    }
}